package cn.antblog.jvm.threadPool;

import java.util.concurrent.*;

/**
 * @ClassName TestFuture
 * @Description 带返回参数
 * @Author YangJingyi
 * @Date 2023/8/3 14:09
 * @Version 1.0
 */
public class TestFuture {
//    public static void main(String[] args) throws InterruptedException, ExecutionException {
//        ExecutorService executor = Executors.newSingleThreadExecutor();   //直接用Executors创建，方便就完事了
//        Future<String> future = executor.submit(() -> "我是字符串!");     //使用submit提交任务，会返回一个Future对象，注意提交的对象可以是Runable也可以是Callable，这里使用的是Callable能够自定义返回值
//        System.out.println(future.get());    //如果任务未完成，get会被阻塞，任务完成返回Callable执行结果返回值
//        executor.shutdown();
//    }

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        ExecutorService executor = Executors.newSingleThreadExecutor();
        Future<String> future = executor.submit(() -> {
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "我是字符串！");
        System.out.println(future.get());
        executor.shutdown();
    }


}
